package com.wisdom.service;

import java.util.ArrayList;

import com.wisdom.VoiceApplication;
import com.wisdom.bean.ContactBean;

import android.app.Service;
import android.content.AsyncQueryHandler;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.provider.ContactsContract;

public class T9Service extends Service {
	
	private AsyncQueryHandler asyncQuery;
	private static final String LOGTAG="T9Service";
	
	public void onCreate(){}
	public void onStart(Intent intent,int startId){
		
	}
	
	public void onDestroy(){
		
	}
	
	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}
	public int onStartCommand(Intent intent,int flags,int startId){
		asyncQuery=new MyAsyncQueryHandler(getContentResolver());
		initSQL();
		
		return super.onStartCommand(intent, flags, startId);
	}
	public void onRebind(Intent intent){
		
	}
	public boolean onUnbind(Intent intent){
		return true;
	}
	private class MyAsyncQueryHandler extends AsyncQueryHandler{

		public MyAsyncQueryHandler(ContentResolver cr) {
			super(cr);
			// TODO Auto-generated constructor stub
		}
		protected void onQueryComplete(int token,Object cookie,Cursor cursor){
			querying(cursor);
		}
		
	}
	private void querying(final Cursor cursor){
		Handler handlerInsertOrder=new Handler(){
			public void handleMessage(Message msg){
				switch(msg.what){
				case MAsyncTask.DOWNLOADING_START_MESSAGE:
					System.out.println("begin to sort out t9");
					break;
				case MAsyncTask.DOWNLOAD_END_MESSAGE:
					Bundle bundle1=msg.getData();
					ArrayList<ContactBean> list=(ArrayList<ContactBean>)bundle1.get("完成");
					VoiceApplication va=(VoiceApplication)getApplication();
					va.setContactBeanList(list);
					break;
					default:
						break;
						
				}
				super.handleMessage(msg);
			}
		};
		MAsyncTask.startRequestServerData(T9Service.this, handlerInsertOrder, cursor);
	}
	protected void initSQL(){
		Uri uri=ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
		String[] projection={
				ContactsContract.CommonDataKinds.Phone._ID,
				ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
				ContactsContract.CommonDataKinds.Phone.DATA1,
				"sort_key",
				ContactsContract.CommonDataKinds.Phone.CONTACT_ID
		};
		asyncQuery.startQuery(0, null, uri, projection, null, null, "sort_key COLLATE LOCALIZED asc");
		
	}

}
